package com.lighting.common.core.vo;

import com.lighting.common.core.util.ObjectUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
public class PageResult<T> {

    private List<T> list;

    private long totalElements;

    private int totalPages;

    public PageResult() {
    }

    public PageResult(List<T> list, long getTotalElements, int getTotalPages) {
        this.list = list;
        this.totalElements = getTotalElements;
        this.totalPages = getTotalPages;
    }

    public static <D> PageResult<D> of(Page<?> page, Class<D> dClass) {
        List<D> convert = page.getContent().stream().map(d -> ObjectUtil.copy(d, dClass)).collect(Collectors.toList());
        return new PageResult<>(convert, page.getTotalElements(), page.getTotalPages());
    }
}
